/*
 * This is the action responsible for listing private messages on
 * the user's profile page.
 */

package airport.forum.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import airport.forum.dao.PrivateMessageDAO;
import airport.forum.pojo.PrivateMessage;

public class ListPrivateMessageAction  extends ActionSupport implements ServletResponseAware,
		ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String sender;
	private String receiver;
	private int messageId;
	private String title;
	private String body;
	private Date dateCreated;
	private Boolean read;

	private List<PrivateMessage> PMs;

	protected HttpServletResponse servletResponse;

	@Override
	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	protected HttpServletRequest servletRequest;

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public String execute() {
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";

		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(cookieName)) {
					receiver = cookies[i].getValue();
					break;
				}
			}
		}

		PrivateMessageDAO dao = new PrivateMessageDAO();

		PMs = dao.getMessageList(receiver);

		return "success";
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

	public Boolean getRead() {
		return read;
	}

	public void setRead(Boolean read) {
		this.read = read;
	}

	public List<PrivateMessage> getPMs() {
		return PMs;
	}

	public void setPMs(List<PrivateMessage> pMs) {
		PMs = pMs;
	}

}
